﻿using AMInstantMessagingServerDao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AMInstantMessagingServerAPI;
using AMInstantMessagingServerAPIModel;
using BaseSimpleAPI;
using BaseDao;
using UDPHelper;

namespace AMInstantMessagingServerAPI
{
    public class UserController : BaseController<UserModel, UserEidtModel, UserViewModel>
    {
        private UserService _UserService = new UserService();

        public override BaseService<UserModel> Service { get { return _UserService; } }

        public override AuthorizationStatus GetAuthorizationStatus(string action, long requestid, long objectid = 0)
        {
            return AuthorizationStatus.Accept;
        }

        /// <summary>
        /// 我是谁
        /// </summary>
        /// <returns></returns>
        public StatusData WhoAmI()
        {
            var user_id = UserTools.GetUserId(UDPContext.Current.RequestUDPPackage);
            
            var user_model = _UserService.GetItemById(user_id);
            if (user_model == null)
                return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().NotFindObjects);

            return StatusData<UserModel>.Success(user_model);
        }

        /// <summary>
        /// 根据账户密码登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public StatusData Login(LoginEditModel model)
        {
            var user_model = _UserService.GetUserByUserName(model.UserName);
            if (user_model == null || user_model.Password != TokenHelper.MD5Encrypt(model.Password))
                return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().UserNameOrPasswordError);

            string token = string.Empty;
            while (true)
            {
                token = TokenHelper.GetInstance().GetRandomKey(32);
                if (_UserService.GetClientInfoByToken(token) == null)
                {
                    _UserService.SetClientInfoAndToken(new UserClientInfo
                    {
                        ClientType = model.ClientType,
                        LoginTime = DateTime.Now,
                        UserId = user_model.Id
                    }, token);

                    break;
                }
            }

            return StatusData<string>.Success(token);
        }

        /// <summary>
        /// 登出
        /// （销毁的当前的小票）
        /// </summary>
        public StatusData Logout()
        {
            // 更新原始小票就可以做到将原本的小票撕毁的作用
            var user_token = UserTools.GetUserToken(UDPContext.Current.RequestUDPPackage);
            var client_info = _UserService.GetClientInfoByToken(user_token);

            if (client_info == null)
                return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().LogoutError);

            _UserService.RemoveClientInfoByToken(user_token);

            return StatusData<string>.Success(StatusDataPrompt.GetStatusDataPrompt().LogoutSuccess);
        }
    }
}
